package com.coin900.coin.cmd.sop;

import com.coin900.coin.cmd.ACmd;
import com.coin900.coin.cmd.CmdSign;
import com.coin900.coin.code.CmdCode;
import com.coin900.coin.persist.fu.FuUserVO;
import com.coin900.coin.persist.sbi.SbiRateVO;
import com.coin900.coin.service.sbi.ISbiRateService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务费率管理列表
 *
 * @author shui
 * @create 2018-01-27
 **/
@Controller
public class SopRatePutCmd extends ACmd {
    @Override
    public CmdSign execute(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        FuUserVO user = super.getUser(sign);
        String source = sign.getSource();
        SbiRateVO sbiRateVO = new Gson().fromJson(source, SbiRateVO.class);
        Long coinId = sbiRateVO.getCoinId();
        Map<String, Object> params = new HashMap<>();
        params.put("coinId", coinId);
        params.put("dataStatus", 1);
        List<SbiRateVO> sbiRateVOS = this.rateService.loadSbiRate(params);
        if (sbiRateVOS.isEmpty()) {
            sbiRateVO.setCreatedInfor(user);
            this.rateService.insertSbiRate(sbiRateVO);
        } else {
            sbiRateVO.setModifiedInfor(user);
            this.rateService.updateSbiRate(sbiRateVO);
        }
        return respSign;
    }

    @Autowired
    private ISbiRateService rateService;

    @Override
    public String getCmdCode() {
        return CmdCode.SOP_RATE_PUT;
    }
}
